<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($specialist!=''){
	$querySpecialist = " AND SpecialistId = '".$specialist."' ";
}else{
	$querySpecialist = "";
}

	$sql="SELECT `equilibrium.price`, `transaction.price.mean`, `ask.placed.price.mean`, `bid.placed.price.mean`, `ask.accepted.price.mean`, `bid.accepted.price.mean` FROM x_score WHERE ExperimentId = '".$Experiment."' ".$querySpecialist." ORDER BY day ASC LIMIT ".$start.",".$items.";";

	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());

	$equilibrium = array();
	$transaction = array();
	$askPlaced = array();
	$bidPlaced = array();
	$askAccepted = array();
	$bidAccepted = array();
	
	$i=0;
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$equilibrium[$i] = round($row['equilibrium.price']);
		$transaction[$i] = round($row['transaction.price.mean']);		
		$askPlaced[$i] = round($row['ask.placed.price.mean']);
		$bidPlaced[$i] = round($row['bid.placed.price.mean']);
		$askAccepted[$i] = round($row['ask.accepted.price.mean']);
		$bidAccepted[$i] = round($row['bid.accepted.price.mean']);

		$i++;		
	} 

$line_equilibrium = new line_hollow();
$line_equilibrium->set_width( 1 );
$line_equilibrium->set_colour( '#5EA725' );
$line_equilibrium->set_dot_size( 3 );
$line_equilibrium->set_values( $equilibrium );
$line_equilibrium->set_tooltip( 'Equilibrium: #val#' );
$line_equilibrium->set_key( "Equilibrium", 10 );

$line_Transaction = new area_line();
$line_Transaction->set_width( 2 );
$line_Transaction->set_colour( '#C82A33' );
$line_Transaction->set_fill_colour( '#F7C9B4' );
$line_Transaction->set_fill_alpha( 0.7 );
$line_Transaction->set_dot_size( 3 );
$line_Transaction->set_values( $transaction );
$line_Transaction->set_tooltip( 'Transaction: #val#' );
$line_Transaction->set_key( "Transaction", 10 );

$line_askPlaced = new line_dot();
$line_askPlaced->set_width( 1 );
$line_askPlaced->set_colour( '#AEA725' );
$line_askPlaced->set_dot_size( 3 );
$line_askPlaced->set_values( $askPlaced );
$line_askPlaced->set_tooltip( 'Ask Placed: #val#' );
$line_askPlaced->set_key( "Ask Placed", 10 );

$line_bidPlaced = new line_dot();
$line_bidPlaced->set_width( 1 );
$line_bidPlaced->set_colour( '#A66765' );
$line_bidPlaced->set_dot_size( 3 );
$line_bidPlaced->set_values( $bidPlaced );
$line_bidPlaced->set_tooltip( 'Bid Placed: #val#' );
$line_bidPlaced->set_key( "Bid Placed", 10 );

$line_askAccepted = new line_dot();
$line_askAccepted->set_width( 1 );
$line_askAccepted->set_colour( '#A667A5' );
$line_askAccepted->set_dot_size( 3 );
$line_askAccepted->set_values( $askAccepted );
$line_askAccepted->set_tooltip( 'Ask Accepted: #val#' );
$line_askAccepted->set_key( "Ask Accepted", 10 );

$line_bidAccepted = new line_dot();
$line_bidAccepted->set_width( 1 );
$line_bidAccepted->set_colour( '#5E4725' );
$line_bidAccepted->set_dot_size( 3 );
$line_bidAccepted->set_values( $bidAccepted );
$line_bidAccepted->set_tooltip( 'Bid Accepted: #val#' );
$line_bidAccepted->set_key( "Bid Accepted", 10 );


$chart = new open_flash_chart();

$chart->set_title( new title( $Experiment.' - Price  -  '.$specialist.' ('.$start.' - '.$final.')' ) );

$x_legend = new x_legend( 'Day' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Price' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );

$x_labels = new x_axis_labels();
$x_labels->set_steps( $step );
//$x_labels->set_vertical();
$x_labels->set_colour( '#000000' );
//$x_labels->set_labels( $items );

$x = new x_axis();
$x->set_colour( '#333333' );
//$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps( $step );
$x->set_range( $start, $final+1 );

$chart->set_x_axis( $x );

$minEquilibrium = min($equilibrium);
$minAskAccepted = min($askAccepted);
$minY = min($minEquilibrium, $minAskAccepted);
$minY = floor($minY/10)*10;
$minY = $minY-5+($minY%5);
if($minY<=0)$minY =0 ;

$maxAskPlaced = max($askPlaced);
$maxBidAccepted = max($bidAccepted);
$maxEquilibrium = max($equilibrium);
$maxY = round(max($maxAskPlaced, $maxBidAccepted, $maxEquilibrium));
$maxY = $maxY+5-($maxY%5);

$differenceY = $maxY - $minY;
$divisorY = $differenceY / 5;
$stepY = $differenceY / $divisorY;

$y = new y_axis();
$chart->set_y_axis( $y );
$y->set_range( $minY, $maxY, $stepY ); 

//
// here we add our data sets to the chart:
//
$chart->add_element( $line_Transaction );
$chart->add_element( $line_equilibrium );
$chart->add_element( $line_askPlaced );
$chart->add_element( $line_askAccepted );
$chart->add_element( $line_bidPlaced );
$chart->add_element( $line_bidAccepted );

echo $chart->toPrettyString();

?>
